#!/usr/bin/python
# -*- coding: utf-8 -*-

import MySQLdb
from class_segment import *
from data_structure import *
import tool_chunk
import numpy
import pprint
import sys
import data_structure
import tool_maths
import time
import math

def get_all_lg(mysql_cursor) :
  req = 'SELECT DISTINCT `lg` FROM `Document`;'
  mysql_cursor.execute(req)

base_db = 'align_ecr_selected'

conn = MySQLdb.connect('localhost','root','ephrael_faith', base_db)
curs = conn.cursor() 

get_all_lg(curs)

corpus_multidoc = {}
id_chunk = 1;

doc2bloc = {}
id_bloc2text = {}
bloc2chunk = {}

curs = [['de'],['el'],['es'],['fi'],['fr'],['pl']]

for lg in curs :
  curs_rel = conn.cursor()
  corres = tool_chunk.lg2relative_id_doc(lg[0],curs_rel)
  for ti,i in corres.iteritems() :
    if corpus_multidoc.has_key(i) == False :
      corpus_multidoc[i] = {}
    corpus_multidoc[i][lg[0]] = ti


for id_multidoc,multidoc in corpus_multidoc.iteritems() :
  for lg,id_doc in multidoc.iteritems() :    
    curs_a = conn.cursor()
    tool_chunk.reader_id_document2array(id_doc,curs_a)
    for info_bloc in curs_a :
      id_bloc = info_bloc[0]
      id_bloc2text[id_bloc] = info_bloc[1]
      curs_w = conn.cursor()
      array_word = tool_chunk.reader_id_bloc2info_chunk(id_bloc,curs_w)
      if (bloc2chunk.has_key(lg) == False) :
        bloc2chunk[lg] = {}
      if (bloc2chunk[lg].has_key(id_doc) == False) :
        bloc2chunk[lg][id_doc] = {}
      bloc2chunk[lg][id_doc][id_bloc] = array_word

id_multidoc = 13
id_multidoc = 1
multidoc = corpus_multidoc[id_multidoc]
lg_src = 'es'
lg_tgt = 'fr'
id_doc_src = multidoc[lg_src]
id_doc_tgt = multidoc[lg_tgt]
curs_corres = conn.cursor()
array_corres_src = tool_chunk.lg2relative_id_bloc(lg_src,curs_corres) 
array_corres_tgt = tool_chunk.lg2relative_id_bloc(lg_tgt,curs_corres)

#print multidoc
#print id_doc_src,id_doc_tgt
list_corres_src = array_corres_src.values()
list_corres_src.sort()
#print list_corres_src

bc_src = bloc2chunk[lg_src][id_doc_src]
bc_tgt = bloc2chunk[lg_tgt][id_doc_tgt]
cpt = 0

list_id_bloc_src = bc_src.keys()
list_id_bloc_src.sort()
list_id_bloc_tgt = bc_tgt.keys()
list_id_bloc_tgt.sort()

assert(len(list_id_bloc_tgt) == len(list_id_bloc_src))

ccpptt = 0
for k in range(0,len(list_id_bloc_tgt)) :
  k = 1
  k1 = list_id_bloc_src[k]
  k2 = list_id_bloc_tgt[k]
  v1 = bc_src[k1]
  v2 = bc_tgt[k2]
  ac_src = tool_chunk.bc2ac(v1,id_bloc2text[k1])
  ac_src = ac_src[0:len(ac_src)-1]
#  ac_src = ac_src[0:7]
  ac_tgt = tool_chunk.bc2ac(v2,id_bloc2text[k2])
  ac_tgt = ac_tgt[0:len(ac_tgt)-1]
#  ac_tgt = ac_tgt[0:9]

  rstr_src = tool_chunk.chunk2rstr_filtered(ac_src,k1,conn)
  rstr_tgt = tool_chunk.chunk2rstr_filtered(ac_tgt,k2,conn)

  to = time.time()

  l_chunk_src = len(rstr_src)
  l_chunk_tgt = len(rstr_tgt)
#  print l_chunk_src,l_chunk_tgt
  chunk_distances = DistDictionnaire(l_chunk_src,l_chunk_tgt)

  for chunk_info_src in rstr_src.iteritems() :
    id_src = chunk_info_src[0]
#    print id_src
    l_src = len(chunk_info_src[1])

    if(l_src == 0) : continue;

    for chunk_info_tgt in rstr_tgt.iteritems() :
      id_tgt = chunk_info_tgt[0]
#      print "   ",id_tgt
      l_tgt = len(chunk_info_tgt[1])

      if(l_tgt == 0) : continue;

      #rstr_distances = DistMatrix(l_src, l_tgt)
      rstr_distances = DistDictionnaire(l_src, l_tgt)
#      print l_tgt,rstr_distances.l2
#      print l_src,rstr_distances.l1
#      print rstr_distances
#      1/0
      tmp_src = []
      for i,info_src in enumerate(chunk_info_src[1]) :
        interval_src = (info_src[2],info_src[2]+info_src[4]-1)
        curs_rep = conn.cursor()
        rep_src = tool_chunk.lg_graphie2repartition_bloc(lg_src,info_src[3],array_corres_src,curs_rep)
        v_src = tool_maths.repartition_bloc2vect(rep_src,list_corres_src)

        tmp_tgt = []
        for j,info_tgt in enumerate(chunk_info_tgt[1]) :
          interval_tgt = (info_tgt[2],info_tgt[2]+info_tgt[4]-1)
          curs_rep = conn.cursor()
          rep_tgt = tool_chunk.lg_graphie2repartition_bloc(lg_tgt,info_tgt[3],array_corres_tgt,curs_rep)
          v_tgt = tool_maths.repartition_bloc2vect(rep_tgt,list_corres_src)

          dist = 1. - tool_maths.get_angle(v_src,v_tgt)
          rstr_distances.set_l1(i,info_src[3])
          rstr_distances.set_l2(j,info_tgt[3])
          rstr_distances.set(i,j,dist)
          rstr_distances.set_info1(i,interval_src)
          rstr_distances.set_info2(j,interval_tgt)
#          rstr_distances.set_all(i, info_src[3], j, info_tgt[3], dist)

#      print rstr_distances.convert2tabular_tex()
#      print rstr_distances.convert2tex()

      rstr_distances.built_class1()
      rstr_distances.built_class2()
      rstr_distances.built_matrix_class()
#      rstr_distance_numpy = rstr_distances.matrix_class2numpy()
#      minDim,maxDim = rstr_distances.get_min_max_class_matrix()
      rstr_distance_numpy = rstr_distances.convert2numpy()
      minDim,maxDim = rstr_distances.get_min_max()
      rstr_distance_numpy = process_threshold_min_max(rstr_distance_numpy,0.0,1)
      matrix_couple,rstr_couple = process_hungarian(rstr_distance_numpy)

      sum_dist = 0.0
      l_scores = []
      for source in xrange(minDim) :
        cible = rstr_couple[source]
        sum_dist += matrix_couple[source][cible]
        l_scores.append(matrix_couple[source][cible])
      nb_scores_valid = math.ceil(len(l_scores) / 4.0)
      nsum_dist = 0.0
      l_scores.sort()
      for i_score in range(int(nb_scores_valid)) :
        nsum_dist += l_scores[i_score]

      score = nsum_dist / nb_scores_valid
#      score = sum_dist / minDim

      chunk_distances.set_l1(id_src,"%s"%(ac_src[id_src]))
      chunk_distances.set_l2(id_tgt,"%s"%(ac_tgt[id_tgt]))
      chunk_distances.set(id_src,id_tgt,score)
#      print "'%s' :: '%s' == %f"%(ac_src[id_src],ac_tgt[id_tgt],score)
#      print "%s :: %s"%(ac_src[id_src].get_offset(),ac_tgt[id_tgt].get_offset())

#  print "%i %i"%(l_chunk_src,l_chunk_tgt)
  
  chunk_numpy = chunk_distances.convert2numpy2()

  chunk_t = chunk_numpy
  chunk_n = chunk_t
#  chunk_n = process_neighbor_model1(chunk_n)
#  chunk_n = process_neighbor_model(chunk_n)
#  chunk_n = process_neighbor_max2(chunk_n)
  mask = process_mathmax(chunk_n,0)
#  if(len(mask) != l_chunk_src and len(mask[0]) != l_chunk_tgt) :
#  print mask
#  mask = zip(*mask)
  chunk_distances.convert4numpy2(chunk_n)
  
#  chunk_distances.convert4numpy(mask)
#  print chunk_distances.convert2tex()
  tex = chunk_distances.convert2tex_mask(mask,1)
  print tex
#  t1 = time.time() - to
#  print "---duration %s : %f"%(k,t1)
  1/0
